package com.mall4j.cloud.order.application.query;

import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall4j.cloud.base.model.BasePageVo;
import com.mall4j.cloud.order.application.model.qo.OrderQo;
import com.mall4j.cloud.order.application.model.vo.OrderVo;
import com.mall4j.cloud.order.infrustructure.persist.dao.OrderMapper;
import com.mall4j.cloud.order.infrustructure.persist.po.OrderPo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.List;

@Service
public class OrderQueryService {

    @Resource
    OrderMapper orderMapper;

    public BasePageVo<OrderVo> list(OrderQo orderQo){
        IPage<OrderPo> page = new Page<>();
        LambdaQueryWrapper<OrderPo> wrapper = Wrappers.lambdaQuery(OrderPo.class).eq(OrderPo::getUserId, orderQo.getUserId());
        IPage<OrderPo> result = orderMapper.selectPage(page, wrapper);
        BasePageVo<OrderVo> basePageVo = new BasePageVo();
        if (CollectionUtils.isNotEmpty(result.getRecords())){
            List<OrderPo> orderPos = result.getRecords();
            List<OrderVo> voList = Lists.newArrayList();
            for (OrderPo po : orderPos){
                OrderVo orderVo = new OrderVo();
                orderVo.setId(po.getId());
                voList.add(orderVo);
            }
            basePageVo.setDataList(voList);
        }
        return basePageVo;
    }

}
